package com.panopset.db;

import java.io.StringWriter;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class SqlSpec {

    private Map<Integer, Object> params;

    public Map<Integer, Object> getParams() {
        if (params == null) {
            params = Collections
                    .synchronizedSortedMap(new TreeMap<Integer, Object>());
        }
        return params;
    }

    private String sql;

    private void add(Integer i, Object o) {
        getParams().put(i, o);
    }

    public String getSql() {
        return sql;
    }

    public void setSql(final String value) {
        sql = value;
    }

    public void setObject(int i, Object o) {
        add(i, o);
    }

    public void setLong(int i, long value) {
        add(i, value);
    }

    public void dumpSql() {
        //System.out.println(this.toString());
    }

    @Override
    public String toString() {
        StringWriter sw = new StringWriter();
        for (Entry<Integer, Object> es : getParams().entrySet()) {
            sw.append("param ");
            sw.append("" + es.getKey());
            sw.append(": ");
            sw.append(es.getValue().toString());
            sw.append("\n");
        }
        sw.append(sql);
        return sw.toString();
    }
}
